﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;

namespace AEM
{
    /// <summary>
    /// Interaction logic for OperName.xaml
    /// </summary>
    public partial class DefinedInfo : Window
    {
        public DefinedInfo()
        {
            InitializeComponent();
            this.TitleBar.MouseLeftButtonDown += new MouseButtonEventHandler(ClientConfig_MouseLeftButtonDown);
            this.btn_save.Click += new RoutedEventHandler(btn_save_Click);
            this.btn_cancel.Click += new RoutedEventHandler(btn_cancel_Click);
            this.Loaded += new RoutedEventHandler(DefinedInfo_Loaded);
        }

        void ClientConfig_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        void DefinedInfo_Loaded(object sender, RoutedEventArgs e)
        {
            LoadInfoToUI();
        }

        private void LoadInfoToUI()
        {
            Data.Model.ClientConfig r = Data.DAL.ClientConfig.Search();
            if (null != r)
            {
                if (null != r.ElevatorInput)
                {
                   List< string> tmp = r.ElevatorInput.Split(',').ToList();
                   if (tmp.Count < 3) { tmp.AddRange((new string[] { "", "", "" })); }
                   if (!string.IsNullOrEmpty(tmp[0])) this.liftInput1.Text = tmp[0];
                   if (!string.IsNullOrEmpty(tmp[1])) this.liftInput2.Text = tmp[1];
                   if (!string.IsNullOrEmpty(tmp[2])) this.liftInput3.Text = tmp[2];
                }
                if (null != r.ElevatorOutput)
                {
                    List<string> tmp = r.ElevatorOutput.Split(',').ToList();
                    if (tmp.Count < 3) { tmp.AddRange((new string[] { "", "", "" })); }
                    if (!string.IsNullOrEmpty(tmp[0])) this.liftOutput1.Text = tmp[0];
                    if (!string.IsNullOrEmpty(tmp[1])) this.liftOutput2.Text = tmp[1];
                    if (!string.IsNullOrEmpty(tmp[2])) this.liftOutput3.Text = tmp[2];
                }
                if (null != r.EscalatorInput)
                {
                    List<string> tmp = r.EscalatorInput.Split(',').ToList();
                    if (tmp.Count < 3) { tmp.AddRange((new string[] { "", "", "" })); }
                    if (!string.IsNullOrEmpty(tmp[0])) this.escalatorInput1.Text = tmp[0];
                    if (!string.IsNullOrEmpty(tmp[1])) this.escalatorInput2.Text = tmp[1];
                    if (!string.IsNullOrEmpty(tmp[2])) this.escalatorInput3.Text = tmp[2];
                }
                if (null != r.WalkwayInput)
                {
                    List<string> tmp = r.WalkwayInput.Split(',').ToList();
                    if (tmp.Count < 3) { tmp.AddRange((new string[] { "", "", "" })); }
                    if (!string.IsNullOrEmpty(tmp[0])) this.walkwayInput1.Text = tmp[0];
                    if (!string.IsNullOrEmpty(tmp[1])) this.walkwayInput2.Text = tmp[1];
                    if (!string.IsNullOrEmpty(tmp[2])) this.walkwayInput3.Text = tmp[2];
                }
            }
        }

        void btn_save_Click(object sender, RoutedEventArgs e)
        {
            string liftinput = this.liftInput1.Text.Trim();
            liftinput += "," + this.liftInput2.Text.Trim();
            liftinput += "," + this.liftInput3.Text.Trim();

            string liftoutput = this.liftOutput1.Text.Trim();
            liftoutput += "," + this.liftOutput2.Text.Trim();
            liftoutput += "," + this.liftOutput3.Text.Trim();

            string escalatorinput = this.escalatorInput1.Text.Trim();
            escalatorinput += "," + this.escalatorInput2.Text.Trim();
            escalatorinput += "," + this.escalatorInput3.Text.Trim();

            string walkwayinput = this.walkwayInput1.Text.Trim();
            walkwayinput += "," + this.walkwayInput2.Text.Trim();
            walkwayinput += "," + this.walkwayInput3.Text.Trim();

            bool ret = AEM.Data.BLL.Actions.DefinedInfo.ModifyDefined(new string[] { liftinput, liftoutput, escalatorinput, walkwayinput });
            if (ret)
            {
                msg.Text = (string)this.TryFindResource("SavedSuccessfully");
                LoadInfoToUI();
            }
            else
                msg.Text = (string)this.TryFindResource("SaveFailed");

        }

        void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void sys_button_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void sys_button_min_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }
    }
}
